/**
 * RefundDaoImp.java
 * 
 * Copyright@2015 OVT Inc. All rights reserved. 
 * 
 * 2015年12月21日
 */
package com.ovt.order.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import com.ovt.common.utils.CollectionUtils;
import com.ovt.order.dao.mapper.RefundMapper;
import com.ovt.order.dao.vo.Refund;

/**
 * RefundDaoImp
 * 
 * @Author leo.liu
 * @Version 1.0
 * @See
 * @Since [OVT Cloud Platform]/[API] 1.0
 */
@Repository
public class RefundDaoImp implements RefundDao
{
    @Autowired
    private DaoHelper daoHelper;

    @Autowired
    private RefundMapper refundMapper;

    private static final String SQL_INSERT_REFUND = "INSERT INTO refund(refund_request_id,"
            + " batch_no, trade_no, create_time) VALUES(?, ?, ?, CURRENT_TIMESTAMP)";

    private static final String SQL_UPDATE_REFUND = "UPDATE refund SET refund_fee = ?, "
            + "refund_state = ?, refund_tax = ?, refund_tax_state = ?, refund_time = ?, "
            + "refund_log_id = ? WHERE batch_no = ? AND trade_no = ? ";

    private static final String SQL_GET_REFUND_PREFIX = "SELECT id, refund_request_id, batch_no,"
            + " trade_no, refund_fee, refund_state, refund_tax, refund_tax_state, refund_time,"
            + " refund_log_id, create_time FROM refund";

    private static final String SQL_GET_REFUND_BY_REQUEST_ID = SQL_GET_REFUND_PREFIX
            + "WHERE refund_request_id = ? LIMIT 1";

    private static final String SQL_GET_REFUND_LIST = SQL_GET_REFUND_PREFIX
            + "";

    private static final String SQL_GET_REQUEST_ID_BY_BATCH_NO = "SELECT refund_request_id"
            + " FROM refund WHERE batch_no = ? ";

    /*
     * (non-Javadoc)
     * 
     * @see com.ovt.dao.RefundDao#save(java.util.List)
     */
    @Override
    public long save(final List<Refund> refunds)
    {
        if (CollectionUtils.isEmpty(refunds))
        {
            return 0;
        }

        String errMsg = "Failed to insert refund list";
        daoHelper.saveBatch(SQL_INSERT_REFUND, errMsg,
                new BatchPreparedStatementSetter()
                {
                    @Override
                    public void setValues(PreparedStatement ps, int i)
                            throws SQLException
                    {
                        Refund refund = refunds.get(i);
                        ps.setLong(1, refund.getRefundRequestId());
                        ps.setString(2, refund.getBatchNo());
                        ps.setString(3, refund.getTradeNo());
                    }

                    @Override
                    public int getBatchSize()
                    {
                        return refunds.size();
                    }
                });

        return 0;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ovt.dao.RefundDao#update(java.util.List)
     */
    @Override
    public void update(final List<Refund> refunds)
    {
        if (CollectionUtils.isEmpty(refunds))
        {
            return;
        }

        String errMsg = "Failed to update refund list";
        daoHelper.saveBatch(SQL_UPDATE_REFUND, errMsg,
                new BatchPreparedStatementSetter()
                {
                    @Override
                    public void setValues(PreparedStatement ps, int i)
                            throws SQLException
                    {
                        Refund refund = refunds.get(i);
                        ps.setFloat(1, refund.getRefundFee());
                        ps.setString(2, refund.getRefundState().toString());
                        ps.setFloat(3, refund.getRefundTax());
                        ps.setString(4, refund.getRefundTaxState());
                        ps.setTimestamp(5, refund.getRefundTime());
                        ps.setLong(6, refund.getRefundLogId());
                        ps.setString(7, refund.getBatchNo());
                        ps.setString(8, refund.getTradeNo());
                    }

                    @Override
                    public int getBatchSize()
                    {
                        return refunds.size();
                    }
                });

    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ovt.dao.RefundDao#getRefund(long)
     */
    @Override
    public Refund getRefund(long refundRequestId)
    {
        String errMsg = MessageFormat.format(
                "Failed to query refund by request id {0}", refundRequestId);

        Refund refund = daoHelper.queryForObject(SQL_GET_REFUND_BY_REQUEST_ID,
                refundMapper, errMsg, refundRequestId);

        return refund;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.ovt.dao.RefundDao#getRefundList()
     */
    @Override
    public List<Refund> getRefundList()
    {
        String errMsg = "Failed to query refund list";

        List<Refund> refundList = daoHelper.queryForList(SQL_GET_REFUND_LIST,
                refundMapper, errMsg);

        return refundList;
    }

    /* (non-Javadoc)
     * @see com.ovt.dao.RefundDao#getRefundRequestIdsByBatchNo(java.lang.String)
     */
    @Override
    public List<Long> getRefundRequestIdsByBatchNo(String batchNo)
    {
        String errMsg = MessageFormat.format(
                "Failed to get refund request ids by batch no : {0}", batchNo);
        
        List<Long> ids = daoHelper.queryForList(
                SQL_GET_REQUEST_ID_BY_BATCH_NO, Long.class, errMsg, batchNo);
        
        return ids;
    }

}
